let router = require('../router/Router');

let Token = require('../token');
let ipUtil = require('../util/ipUtil');

let userService = require('../service/userService');
let unionService = require('../service/UnionService')

const {BusiError, SysError} = require('../exception/exception');


/**
 * 修改大联盟的抽水设置
 */
router.post('/auth/user/union/updateRoyaltySetting', (req, resp) => {
    let uid = req.uid;
    let unionId = req.body.unionId;
    let data = req.body.data;
    unionService.updateUnionRoyaltySetting(uid, unionId, data, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        console.logw(e);
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});

/**
 * 查询大联盟信息
 */
router.post('/auth/user/queryUnion', (req, resp) => {
    let uid = req.uid;
    let unionId = req.body.unionId;
    unionService.getUnionInfo(uid, unionId, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        console.logw(e);
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});


/**
 * 保存大联盟的抽水记录
 */
router.post('/game/union/royalty/record/save', (req, resp) => {
    let recordList = req.body.data;
    unionService.saveUnionRoyaltyRecord(recordList, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        console.logw(e)
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});

/**
 * 按天获取大联盟的抽水记录
 */
router.post('/auth/union/royalty/record/list', (req, resp) => {
    let date = req.body.date;
    let unionId = req.body.unionId;
    let clubId = req.body.clubId;
    unionService.getUnionRoyaltyRecordList(date, unionId,clubId, req.ip, req.ua).then(res => {
        let result = {};
        result.code = 200;
        result.msg = "success";
        result.data = res;
        resp.send(result);
    }).catch(e => {
        let msg = "系统异常, 请稍后再试";
        console.logw(e)
        if (e instanceof BusiError) {
            msg = e.message;
        }
        resp.send({ code: 500, msg: msg });
    });
});